package org.tinygroup.parsedsql.parser;

import net.sf.cglib.proxy.Enhancer;

import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.parsedsql.exception.ParsedSqlException;

/**
 * SQL解析日志打印.
 * 
 * @author renhui
 */
public final class VisitorLogProxy {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(VisitorLogProxy.class);

	private VisitorLogProxy() {
		super();
	}

	/**
	 * 打印SQL解析调用树.
	 * 
	 * @param target
	 *            待增强类
	 * @param <T>
	 *            泛型
	 * @return 增强后的新类的对象
	 */
	@SuppressWarnings("unchecked")
	public static <T> T enhance(final Class<T> target) {
		if (LOGGER.isEnabled(LogLevel.TRACE)) {
			Enhancer result = new Enhancer();
			result.setSuperclass(target);
			result.setCallback(new VisitorHandler());
			return (T) result.create();
		} else {
			try {
				return target.newInstance();
			} catch (final InstantiationException ex) {
				LOGGER.errorMessage("create Visitor exception", ex);
				throw new ParsedSqlException(ex);
			} catch (IllegalAccessException ex) {
				LOGGER.errorMessage("create Visitor exception", ex);
				throw new ParsedSqlException(ex);
			}
		}
	}

}
